//
//  QSheetQAlertView.m
//  XinKaiFa55like
//
//  Created by 55like on 2017/9/22.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "QSheetQAlertView.h"

@interface SheetTableViewCell : UITableViewCell

@property (nonatomic,strong)UILabel * title;

@end

@implementation SheetTableViewCell

-(UILabel *)title{
    
    if (!_title) {
        
        UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frameWidth, self.frameHeight)];
        lab.textColor = rgbTitleDeepGray;
        lab.font = Font(15);
        lab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:lab];
        _title = lab;
        
        
    }
    
    return _title;
}

@end


@interface QSheetQAlertView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView * zhltableView;
@property (nonatomic,strong)NSArray * dataArray;
//每一行高度
@property (nonatomic,copy)CGFloat(^cellHeightBlock)(NSInteger row);
//点击回调
@property (nonatomic,copy)void(^cellClickIndex)(NSInteger index);
//取消按钮标题
@property (nonatomic,copy)NSString * cancelStr;

@property (nonatomic,assign)CGFloat cellHeight;

@end



@implementation QSheetQAlertView

+(QSheetQAlertView *)showQSheetQAlertViewWithTitleArray:(NSArray *)dataArray cellClickIndex:(void(^)(NSInteger index))cellClickIndex{
    
    return [QSheetQAlertView showQSheetQAlertViewWithTitleArray:dataArray cancelStr:@"取消" cellHeight:50 cellHeightBlock:nil cellClickIndex:cellClickIndex];
}



+(QSheetQAlertView *)showQSheetQAlertViewWithTitleArray:(NSArray *)dataArray cancelStr:(NSString *)cancelStr cellHeight:(CGFloat)cellHeight cellHeightBlock:(CGFloat(^)(NSInteger row))cellHeightBlock cellClickIndex:(void(^)(NSInteger index))cellClickIndex{
    
    QSheetQAlertView * sheetview = [[QSheetQAlertView alloc]init];
    sheetview.dataArray = dataArray;
    sheetview.cancelStr = cancelStr;
    sheetview.cellHeight = cellHeight;
    sheetview.cellHeightBlock = cellHeightBlock;
    sheetview.cellClickIndex = cellClickIndex;
    sheetview.topHide = YES;
    [sheetview setterUI];
    [sheetview show];
    
    return sheetview;
}


//初始化
-(void)setterUI{
    
   
    self.backgroundView.frame = self.bounds;
    if (self.topHide == YES) {
        
        self.backgroundView.userInteractionEnabled = YES;
        
        
    }else{
        
        self.backgroundView.userInteractionEnabled = NO;
        
    }
    CGFloat h = self.cellHeight * self.dataArray.count + [self tableivewFooterView].frameHeight;
    self.mainView.backgroundColor = [UIColor clearColor];
    self.mainView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, h);
    self.zhltableView.frame = CGRectMake(0, 0, kScreenWidth, h);
    
    
    
    
}
//隐藏
- (void)hide{
    
 

    [UIView animateWithDuration:0.3 animations:^{
        
        self.mainView.frameY = QA_SCREEN_H;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
    
}
//显示
-(void)show{
    
    CGFloat h = self.cellHeight * self.dataArray.count + [self tableivewFooterView].frameHeight;
    CGFloat y = kScreenHeight - h;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.mainView.frameY = y;
        
    } completion:^(BOOL finished) {
        
        
    }];
    
    
}
static NSString * SheetTableViewCellID  = @"SheetTableViewCellID";

-(UITableView *)zhltableView{
    
    if (!_zhltableView) {
        
        UITableView * zhltableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStyleGrouped];
        zhltableView.delegate = self;
        zhltableView.dataSource = self;
        zhltableView.scrollEnabled = NO;
        zhltableView.backgroundColor = [UIColor clearColor];
         zhltableView.separatorInset=UIEdgeInsetsMake(0,0, 0, 0);
        zhltableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        zhltableView.showsVerticalScrollIndicator = NO;
        zhltableView.showsHorizontalScrollIndicator = NO;
        [self.mainView addSubview:zhltableView];
        [zhltableView registerClass:[SheetTableViewCell class] forCellReuseIdentifier:SheetTableViewCellID];
        _zhltableView = zhltableView;
    }
    _zhltableView.tableFooterView = self.tableivewFooterView;
    
    return _zhltableView;
}
-(UIView *)tableivewFooterView{
    
    UIView * tableivewFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth,self.cellHeight + 10)];
    tableivewFooterView.backgroundColor = [UIColor clearColor];
    UILabel * title = [RHMethods labelWithFrame:CGRectMake(0, 10, kScreenWidth, self.cellHeight) font:Font(15) color:rgbTitleDeepGray text:self.cancelStr?self.cancelStr:@"取消"];
    title.backgroundColor = [UIColor whiteColor];
    [tableivewFooterView addSubview:title];
    title.textAlignment = NSTextAlignmentCenter;
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, tableivewFooterView.frameWidth, tableivewFooterView.frameHeight)];
    [button addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [tableivewFooterView addSubview:button];
    return tableivewFooterView;
}
#pragma mark -  tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    return self.dataArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return self.cellHeightBlock?self.cellHeightBlock(indexPath.row):self.cellHeight;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section    {
    
    
    
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView * headerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    
    
    return headerview;
    
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    
    UIView * footerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
    footerview.backgroundColor = [UIColor whiteColor];
    
    
    
    return footerview;
    
    
}
#pragma mark  cell处理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SheetTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SheetTableViewCellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.title.text = self.dataArray[indexPath.row];
    
    
    
    
    
    
    
    return cell;
    
}
#pragma mark  选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSLog(@"%@",indexPath);
    
    
        
    if (self.cellClickIndex) {
            
        self.cellClickIndex(indexPath.row);
    }
    [self hide];
    
}
-(NSArray *)dataArray{
    
    if (!_dataArray) {
        
        _dataArray = [[NSArray alloc]init];
        
    }
    
    return _dataArray;
}


@end
